﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Problem2
{
    class Tablero
    {
        int[][] tablero;
        int largo = 10;
        int ancho = 6;
        public Tablero()
        {
            tablero = new int[largo][];
            for (int i = 0; i < tablero.Length; i++)
            {
                tablero[i] = new int[ancho];
                for (int j = 0; j < tablero[i].Length; j++)
                {
                    tablero[i][j] = 0;
                }
            }

        }


        public coordenadas[] putFichaV2(coordenadas[] coords, coordenadas coord, int figId)
        {
            int difx = coord.GetX() - coords[0].GetX();
            int dify = coord.GetY() - coords[0].GetY();
            coordenadas[] puestas = new coordenadas[5];
            for (int i = 0; i < 5; i++)
            {
                int newx = coords[i].GetX() + difx;
                int newy = coords[i].GetY() + dify;
                if (newx < 0 || newx > 5 || newy < 0 || newy > 9)
                    return null;
                else
                    if (tablero[newy][newx] > 0)
                        return null;
                    else
                        puestas[i] = new coordenadas(newx, newy);
            }
            for (int i = 0; i < 5; i++)
            {
                tablero[puestas[i].GetY()][puestas[i].GetX()] = figId;
            }
            return puestas;

        }


        public bool putFicha(figura fig, coordenadas coord2)
        {
            coordenadas coord = new coordenadas(coord2.GetX(), coord2.GetY());
            for (int i = 0; i < fig.getMatrix()[0].Length; i++)
            {
                if (fig.getMatrix()[0][i] == 0)
                {
                    coord.SetX(coord.GetX() - 1);
                    if (coord.GetX() < 0)
                        return false;
                }
                else
                {
                    break;
                }
            }
            if (fig.getLengthH() + coord.GetX()  > ancho || fig.getLengthV() + coord.GetY()  > largo)
                return false;
            else
            {
                int lengthV = fig.getLengthV();
                int lengthH = fig.getLengthH();
                for (int i = 0; i < lengthV; i++)
                    for (int j = 0; j < lengthH; j++)
                        if (tablero[coord.GetY() + i][coord.GetX() + j] > 0 && fig.getMatrix()[i][j] > 0)
                            return false;
                for (int i = 0; i < lengthV; i++)
                    for (int j = 0; j < lengthH; j++)
                        if (fig.getMatrix()[i][j] > 0)
                            tablero[coord.GetY() + i][coord.GetX() + j] = fig.getMatrix()[i][j];
                return true;
            }
        }

        public void pullFichaV2(coordenadas[] coords)
        {
            for (int i = 0; i < coords.Length; i++)
                tablero[coords[i].GetY()][coords[i].GetX()] = 0;
        }




        public void pullFicha(figura fig)
        {
            for (int i = 0; i < this.largo; i++)
                for (int j = 0; j < this.ancho; j++)
                    if (tablero[i][j] == fig.getId())
                        tablero[i][j] = 0;
        }

        public int getCasilla(coordenadas coord)
        {
            return tablero[coord.GetY()][coord.GetX()];
        }

        public coordenadas getNextLibre(coordenadas coord)
        {
            for (int j = coord.GetX(); j < this.ancho; j++)
                if (tablero[coord.GetY()][j] == 0)
                    return new coordenadas(j, coord.GetY());

            for (int i = coord.GetY(); i < this.largo; i++)
                for (int j = 0; j < this.ancho; j++)
                    if (tablero[i][j] == 0)
                        return new coordenadas(j, i);
            return null;
        }

        public bool isFull()
        {
            for (int i = 0; i < this.largo; i++)
                for (int j = 0; j > this.ancho; j++)
                    if (tablero[i][j] == 0)
                        return false;
            return true;
        }

        public void printTab()
        {
            Console.WriteLine("**************TABLERO******************");
            for (int i = 0; i < this.largo; i++)
            {
                Console.Write("|");
                for (int j = 0; j < this.ancho; j++)
                {
                    if (tablero[i][j] < 10)
                        Console.Write("0");
                    Console.Write(tablero[i][j]+"|");
                }
                Console.WriteLine("\n");
            }
            Console.WriteLine("****************************************");

        }

        public string[] getTab()
        {
            String res1 = "";
            String res2 = "";
            String res3 = "";
            String res4 = "";
            for (int i = 0; i < this.largo; i++)
            {
                
                for (int j = 0; j < this.ancho; j++)
                {
                    res1 += tablero[i][j].ToString();
                }
                for (int j = this.ancho - 1; j >= 0; j--)
                {
                    res2 += tablero[i][j].ToString();
                }

            }
            for (int i = this.largo - 1; i >= 0; i--)
            {

                for (int j = 0; j < this.ancho; j++)
                {
                    res3 += tablero[i][j].ToString();
                }
                for (int j = this.ancho - 1; j >= 0; j--)
                {
                    res4 += tablero[i][j].ToString();
                }

            }
            String[] res = new String[4];
            res[0] = res1;
            res[1] = res2;
            res[2] = res3;
            res[3] = res4;
            return res;
        }

        public bool verifica(coordenadas coord2)
        {
            //coordenadas coord;
            return true;
            /*if (coord2 != null)
                coord = new coordenadas(coord2.GetX(), coord2.GetY());
            else
                return true;
                //coord = new coordenadas(ancho - 1, largo - 1);
            for (int i = 0; i <= coord.GetY(); i++)
            {
                int maxj;
                if (i < coord.GetY())
                    maxj = this.ancho - 1;
                else
                    maxj = coord.GetX() - 1;
                for (int j = 0; j <= maxj; j++)
                {
                    if (this.tablero[i][j] == 0)
                        return false;
                }
                

            }
            return true;*/
        }

    }
}
